package com.sh.redis.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class minimumDifference {

    public static void main(String[] args) {
        int[] a = new int[]{9,4,1,7};
        System.out.println(minimumDifference(a,2));
    }


    public static int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);
        int result = nums[nums.length-1]-nums[0];
        for(int i= 0 ;i<nums.length-k+1;i++){
           if(result > nums[i+k-1]-nums[i]){
               result = nums[i+k-1]-nums[i];
           }
        }
        return result;
    }

}
